/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poproject.GUI;

import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jan Karaś
 */
public class FootballTeamPanel extends javax.swing.JPanel {

    private DefaultTableModel mModel;       //< Model tablicy drużyn znajdującej się w GUI
    private MainWindow mMainWindow;         //< Zmienna reprezentująca główne okno programu
    private PTConfigWindow mConfigWindow;   //< Zmienna reprezentująca okno na którym jest wyświetlany panel
    
    /**
     * Konstruktor tworzący instancję panelu.
     * @param mainWindow Główne okno aplikacji
     * @param configWindow Okno na którym wyswietlany jest panel
     */
    public FootballTeamPanel(MainWindow mainWindow, PTConfigWindow configWindow) {
        initComponents();
        mMainWindow = mainWindow;
        mConfigWindow = configWindow;
        mModel = (DefaultTableModel)this.jTableTeams.getModel();                   
    }
    
    /**
     * Tabela drużyn.
     * Metoda pozwalająca na pobranie tabeli drużyn wprowadzonych przez użytkownika
     * w GUI.
     * @return Tabela drużyn
     */
    public JTable getTeamsTable()
    {
        return this.jTableTeams;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableTeams = new javax.swing.JTable();
        jButtonAdd = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButtonDelete = new javax.swing.JButton();
        jButtonClose = new javax.swing.JButton();
        jButtonClear = new javax.swing.JButton();
        jButtonApply = new javax.swing.JButton();
        jButtonExample = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(546, 317));

        jTableTeams.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null}
            },
            new String [] {
                "Drużyna", "Boisko", "Max. ilość meczy dziennie"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTableTeams.getTableHeader().setReorderingAllowed(false);
        jTableTeams.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTableTeamsKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTableTeams);

        jButtonAdd.setText("Dodaj");
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Opcje");

        jButtonDelete.setText("Usuń");
        jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteActionPerformed(evt);
            }
        });

        jButtonClose.setText("Zamknij");
        jButtonClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCloseActionPerformed(evt);
            }
        });

        jButtonClear.setText("Wyczyść");
        jButtonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonClearActionPerformed(evt);
            }
        });

        jButtonApply.setText("Zatwierdź");
        jButtonApply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonApplyActionPerformed(evt);
            }
        });

        jButtonExample.setText("Przykład");
        jButtonExample.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExampleActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(jLabel1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jButtonAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonDelete, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonApply, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonExample, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonClose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonDelete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonClear)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonExample)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 138, Short.MAX_VALUE)
                        .addComponent(jButtonApply)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonClose)
                        .addGap(0, 0, 0)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Dodanie wiersza w tabeli.
     * @param evt 
     */
    private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
        // TODO add your handling code here:
        mModel.insertRow(jTableTeams.getRowCount(), new Object [] {null, null});   
        //mModel.insertRow(0, new Object [] {"B", "B", "B"});  
    }//GEN-LAST:event_jButtonAddActionPerformed

    /**
     * Usunięcie wiersza z tabeli.
     * @param evt 
     */
    private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteActionPerformed
        // TODO add your handling code here:
        try
        {
            mModel.removeRow(jTableTeams.getSelectedRow());
        }
        catch(Exception e)
        {
            String str = "Nie zaznaczono żadnego wiersza!";
            JOptionPane.showMessageDialog(this, str, "OSTRZEŻENIE", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButtonDeleteActionPerformed

    /**
     * Dodanie wiersza w tabeli za pomocą przycisku INSERT.
     * @param evt 
     */
    private void jTableTeamsKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTableTeamsKeyReleased
        // TODO add your handling code here:
        if(evt.getKeyCode() == KeyEvent.VK_INSERT)
            jButtonAddActionPerformed(null);
    }//GEN-LAST:event_jTableTeamsKeyReleased

    /**
     * Zamknięcie aplikacji.
     * Zapisa danych wprowadzonych w panelu i przesłanie ich do głównego okna 
     * aplikacji. Zamknięcie okna edycyjnego.
     * @param evt 
     */
    private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseActionPerformed
        // TODO add your handling code here:
        this.mMainWindow.setTeamsTable(jTableTeams);
        this.mConfigWindow.setVisible(false);
    }//GEN-LAST:event_jButtonCloseActionPerformed

    /**
     * Wyczyszczenie zawartości tabeli.
     * @param evt 
     */
    private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonClearActionPerformed
        // TODO add your handling code here:
        this.mModel.setRowCount(0);
    }//GEN-LAST:event_jButtonClearActionPerformed

    /**
     * Zatwierdzenie danych wprowadzonych przez użytkownika w Panelu.
     * @param evt 
     */
    private void jButtonApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonApplyActionPerformed
        // TODO add your handling code here:
        this.mMainWindow.setTeamsTable(jTableTeams);
    }//GEN-LAST:event_jButtonApplyActionPerformed

    /**
     * Załadowanie przykładowego zestawu druzyn.
     * @param evt 
     */
    private void jButtonExampleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExampleActionPerformed
        // TODO add your handling code here:
        this.mModel.setRowCount(0);
        this.mModel.addRow(new Object[]{"Wisła", "Kraków", 1});
        this.mModel.addRow(new Object[]{"Legia", "Warszawa", 1});
        this.mModel.addRow(new Object[]{"Piast", "Gliwice", 1});
        this.mModel.addRow(new Object[]{"Górnik", "Zabrze", 1});
        this.mModel.addRow(new Object[]{"Widzew", "Lódź",1});
        this.mModel.addRow(new Object[]{"Ruch", "Chorzów", 1});
        /*this.mModel.addRow(new Object[]{"Arka", 1});
        this.mModel.addRow(new Object[]{"Lech", 1});
        this.mModel.addRow(new Object[]{"Lechia", 1});
        this.mModel.addRow(new Object[]{"Piast", 1});
        this.mModel.addRow(new Object[]{"Korona", 1});
        this.mModel.addRow(new Object[]{"Pogon", 1});
        this.mModel.addRow(new Object[]{"Belchatow", 1});*/
        
    }//GEN-LAST:event_jButtonExampleActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonApply;
    private javax.swing.JButton jButtonClear;
    private javax.swing.JButton jButtonClose;
    private javax.swing.JButton jButtonDelete;
    private javax.swing.JButton jButtonExample;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableTeams;
    // End of variables declaration//GEN-END:variables
}
